package com.demo.aop;

import com.demo.config.DataSourceDialect;
import com.demo.sourceSwiitch.DataSourceSwitch;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class AddressAop {

    private Logger logger= LogManager.getLogger(AddressAop.class);

    @Pointcut("execution(* com.demo.controller.AddressController.*(..))")
    private void allMethod(){}

    //针对指定的切入点表达式选择的切入点应用前置通知
    //@Before("allMethod()")
    public void before(JoinPoint call){
        String className = call.getTarget().getClass().getName();
        String methodName = call.getSignature().getName();
        logger.info("【注解-前置通知】:" + className + "类的"
                + methodName + "方法开始了");

        DataSourceSwitch.setDataSourceType(DataSourceDialect.TEST2);
    }
    @Around("allMethod()")
    public Object doAround(ProceedingJoinPoint call) throws Throwable{
        Object result = null;
        this.before(call);//相当于前置通知
        try {
            result = call.proceed();
        } catch (Throwable e) {
            throw e;
        }finally{
        }
        return result;
    }
}

